<template>
  <div>
    <el-card>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>{{ $route.meta.firstTitle }}</el-breadcrumb-item>
        <el-breadcrumb-item>{{ $route.meta.secondTitle }}</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 分割线 -->
      <el-divider></el-divider>
      <el-form label-width="80px">
        <el-form-item label="姓名:">
          <el-input v-model="student.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄:">
          <el-input v-model="student.age"></el-input>
        </el-form-item>
        <el-form-item label="性别:">
          <el-radio-group v-model="student.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="专业">
          <el-select placeholder="请选择专业" v-model="student.subjectsId"> 
            <el-option v-for="item in subjects" :key="item._id" :label="item.name" :value="item._id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专业">
          <el-select placeholder="请选择专业" v-model="student.classesId"> 
             <el-option v-for="item in classes" :key="item._id" :label="item.name" :value="item._id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像上传">
            <el-upload
                class="avatar-uploader"
                action="http://www.zhaijizhe.cn:3005/images/uploadImages"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="student.image" :src="student.image" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
        </el-form-item>
      </el-form>

    </el-card>
  </div>
</template>

<script>
export default {
  name: "StudentsAdd",
  data() {
    return {
      student: {
        name: "",
        age: "",
        gender:"男",
        classesId:'',
        subjectsId:'',
        image:''
      },
      subjects:[],
      classes:[]
    };
  },
  methods:{
    async getSubjects(){
      const {data:{result}}=await this.$api.subjects.getSubjects()
      this.subjects=result
    },
    beforeAvatarUpload(file){
        const isJPG = file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 PNG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
    },
    handleAvatarSuccess(res, file){
      this.student.image=`http://www.zhaijizhe.cn:3005/${res.data[0]}`
      console.log(this.student.image);
    }
  },
   watch:{
      async "student.subjectsId"(newval){
         const {data:{result}}=await this.$api.classes.getClassesBySubjectsId(newval)
         this.classes=result
       } 
   },
  created(){
    this.getSubjects()
  }
};
</script>

<style scoped>
   .avatar-uploader ::v-deep .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader ::v-deep .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>